﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace _6_05_多窗体实现数据增删改查_窗体传值_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void BindData()
        {
            //连接数据库
            string conn = "server=.;database=TestDB;uid=sa;pwd=200234.A";
            SqlConnection sqlconn = new SqlConnection(conn);
            sqlconn.Open();
            string sql = "select * from Member";
            SqlDataAdapter adp = new SqlDataAdapter(sql, sqlconn);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            this.dataGridView1.DataSource = dt;

            sqlconn.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.BindData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AddFrm addFrm = new AddFrm();
            //所有者指向总窗体实例
            addFrm.Owner = this;
            addFrm.ShowDialog();

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string conn = "server=.;database=TestDB;uid=sa;pwd=200234.A";
            SqlConnection sqlConnection = new SqlConnection(conn);
            sqlConnection.Open();
            string sql = $"delete   from Member where MemberId=" +
                $" '{this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString()}'";
            SqlCommand sqlcmd = new SqlCommand(sql, sqlConnection);
            int count = sqlcmd.ExecuteNonQuery();
            if (count > 0)
            {
                MessageBox.Show("删除成功");
            }
            else
            {
                MessageBox.Show("删除失败");
            }
            sqlConnection.Close();
            this.BindData();
        }


        private void 删除ToolStripMenuItem_Click_2(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.YesNo);
            if (dr == DialogResult.No) {
                return;
            }
            string conn = "server=.;database=TestDB;uid=sa;pwd=200234.A";
            SqlConnection sqlConnection = new SqlConnection(conn);
            sqlConnection.Open();
            //string sql = $"delete   from Member where MemberId=" +
            //    $" '{this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString()}'";
            int MemberId = int.Parse(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            string sql = "delete from Member where MemberId = @MemberId";
            SqlCommand sqlcmd = new SqlCommand(sql, sqlConnection);
            sqlcmd.Parameters.Add(new SqlParameter("@MemberId",MemberId));
            int count = sqlcmd.ExecuteNonQuery();
            if (count > 0)
            {
                MessageBox.Show("删除成功");
            }
            else
            {
                MessageBox.Show("删除失败");
            }
            sqlConnection.Close();
            this.BindData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int Menid = int.Parse(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            EditFrm editFrm = new EditFrm(Menid);
            editFrm.Owner = this;
            editFrm.ShowDialog();
        }
    }
}
